package com.prestamomo.vil.ppa.pmvilpage;

import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.prestamomo.vil.ppa.R;
import com.prestamomo.vil.ppa.pmvilapi.EventMessage;
import com.prestamomo.vil.ppa.pmvilapi.PmvilBanks;
import com.prestamomo.vil.ppa.pmvilapi.PmvilBodys;
import com.prestamomo.vil.ppa.pmvilapi.PmvilClient;
import com.prestamomo.vil.ppa.pmvilapi.PmvilHttpRequest;
import com.prestamomo.vil.ppa.pmvilapi.PmvilSerConfig;
import com.prestamomo.vil.ppa.pmvilapi.PmvilUserRz;
import com.prestamomo.vil.ppa.pmvilmedium.pmviluse.PmvilTosMd;
import com.prestamomo.vil.ppa.pmvilnet.PmvilAjustinfse;
import com.prestamomo.vil.ppa.pmvilnet.pmvillistener.PmvilCallBack;
import com.prestamomo.vil.ppa.pmvilpage.pmfitter.PmvilBankFiter;
import com.prestamomo.vil.ppa.pmvilpage.pmfitter.PmvilSourceFiter;
import com.prestamomo.vil.ppa.pmvilpage.pmrot.PmvilAtyRoot;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

public class PmvilBlockVy extends PmvilAtyRoot {

    private ImageView blockPmvilIvback;
    private TextView blockPmvilTvname;
    private LinearLayout blockPmvilLlbank;
    private TextView blockPmvilTvbank;
    private LinearLayout blockPmvilLlbankType;
    private TextView blockPmvilTvbankType;
    private EditText blockPmvilEtnum;
    private EditText blockPmvilEtnumagen;
    private TextView blockPmvilTvphone;
    private TextView blockPmvilTvtj;
    private List<PmvilBanks.BankListPmvil> pmvilbankList = new ArrayList<>();
    private List<PmvilSerConfig.ExtInfoArrPmvil> pmvilextInfoArr = new ArrayList<>();
    private String pmvilBankTypecode;
    private PmvilBanks.BankListPmvil pmvilBankbean;

    @Override
    protected int getPmvilLayoutId() {
        return R.layout.block_pmvil_ly;
    }

    @Override
    protected void showPmvilUi() {
        sendAdjPmvilEvent(PmvilAjustinfse.pmvil_lyygnrge);
        sendAdjPmvilEvent(PmvilAjustinfse.pmvil_lyygnrge_0);
        blockPmvilIvback = findViewById(R.id.block_pmvil_iv_back);
        blockPmvilTvname = findViewById(R.id.block_pmvil_tv_name);
        blockPmvilLlbank = findViewById(R.id.block_pmvil_ll_bank);
        blockPmvilTvbank = findViewById(R.id.block_pmvil_tv_bank);
        blockPmvilLlbankType = findViewById(R.id.block_pmvil_ll_bank_type);
        blockPmvilTvbankType = findViewById(R.id.block_pmvil_tv_bank_type);
        blockPmvilEtnum = findViewById(R.id.block_pmvil_et_num);
        blockPmvilEtnumagen = findViewById(R.id.block_pmvil_et_num_agen);
        blockPmvilTvphone = findViewById(R.id.block_pmvil_tv_phone);
        blockPmvilTvtj = findViewById(R.id.block_pmvil_tv_tj);

    }

    @Override
    protected void clickePmvil() {
        blockPmvilIvback.setOnClickListener(v -> showPmvilWaliuDlg());
        blockPmvilLlbank.setOnClickListener(v -> showBankPmvilListDgl(blockPmvilLlbank,blockPmvilTvbank,"Tipo de banco",pmvilbankList));
        blockPmvilLlbankType.setOnClickListener(v -> showBankPmvilTypeDgl(blockPmvilLlbankType,blockPmvilTvbankType,"Tipo de cuenta",pmvilextInfoArr));

        blockPmvilTvtj.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(TextUtils.isEmpty(blockPmvilTvbank.getText().toString())||TextUtils.isEmpty(blockPmvilTvbankType.getText().toString())||
                        TextUtils.isEmpty(blockPmvilEtnum.getText().toString())||TextUtils.isEmpty(blockPmvilEtnumagen.getText().toString())){
                    PmvilTosMd.showPmvaMsg("資訊不完整");
                }else if (!blockPmvilEtnum.getText().toString().equals(blockPmvilEtnumagen.getText().toString())){
                    PmvilTosMd.showPmvaMsg("輸入的帳號不一致");
                }else{
                    sendAdjPmvilEvent(PmvilAjustinfse.pmvil_bbrtgrkjtj);
                    sendAdjPmvilEvent(PmvilAjustinfse.pmvil_bbrtgrkjtj_0);
                    comitBankPmvilInfo();
                }
            }
        });
    }

    private void getPmvilQueryUser() {
        showPmvilLoading();
        PmvilHttpRequest.getPmvilQueryUser(this,  new PmvilCallBack<PmvilUserRz>() {
            @Override
            public void onPmvilSucc(String pmvilMethod, PmvilUserRz pmvilContent) {
                hindPmvilLoading();
                if (pmvilContent.getBindCardStatus().equals("0")) {
                    EventBus.getDefault().post(new EventMessage());
                    finish();
                }
            }

            @Override
            public void onPmvilLogOn(String pmvilMod, PmvilUserRz pmvilCtent) {
                outpmvilLongin();
            }

            @Override
            public void onPmvilOther(String pmvilMhod, PmvilUserRz pmvilCnt, String pmvilCode, String pmvilMsg) {
                tostPmvilErrMsg(pmvilMsg);


            }

            @Override
            public void onPmvilFailure(String pmvilMod, String pmvilError) {
                tostPmvilErrMsg(pmvilError);
            }
        });
    }
    private void comitBankPmvilInfo() {
        showPmvilLoading();
        PmvilHttpRequest.postBankPmvilInfo(this,blockPmvilTvname.getText().toString(), pmvilBankbean, pmvilBankTypecode, blockPmvilEtnum.getText().toString(), new PmvilCallBack<PmvilBodys>() {
            @Override
            public void onPmvilSucc(String pmvilMethod, PmvilBodys pmvilContent) {
                sendAdjPmvilEvent(PmvilAjustinfse.pmvil_cvvfsee_t);
                sendAdjPmvilEvent(PmvilAjustinfse.pmvil_cvvfsee_t_0);
                sendAdjPmvilEvent(PmvilAjustinfse.pmvil_jjherwew_t);
                sendAdjPmvilEvent(PmvilAjustinfse.pmvil_jjherwew_t_0);
//                getCheckPmvilPage(true);
                getPmvilQueryUser();
            }

            @Override
            public void onPmvilLogOn(String pmvilMod, PmvilBodys pmvilCtent) {
                outpmvilLongin();
            }

            @Override
            public void onPmvilOther(String pmvilMhod, PmvilBodys pmvilCnt, String pmvilCode, String pmvilMsg) {
                tostPmvilErrMsg(pmvilMsg);
                sendAdjPmvilEvent(PmvilAjustinfse.pmvil_sfwesd_e);
                sendAdjPmvilEvent(PmvilAjustinfse.pmvil_sfwesd_e_0);
            }

            @Override
            public void onPmvilFailure(String pmvilMod, String pmvilError) {
                tostPmvilErrMsg(pmvilError);
            }
        });
    }

    @Override
    protected void loadPmvilData() {
        showPmvilLoading();
        PmvilHttpRequest.getUserPmvilData(this, new PmvilCallBack<PmvilClient>() {
            @Override
            public void onPmvilSucc(String pmvilMethod, PmvilClient pmvilContent) {
                blockPmvilTvphone.setText(pmvilContent.getMobile());
                blockPmvilTvname.setText(pmvilContent.getUserName());
                getBankPmvilList();
            }

            @Override
            public void onPmvilLogOn(String pmvilMod, PmvilClient pmvilCtent) {
                outpmvilLongin();
            }

            @Override
            public void onPmvilOther(String pmvilMhod, PmvilClient pmvilCnt, String pmvilCode, String pmvilMsg) {
                tostPmvilErrMsg(pmvilMsg);
            }

            @Override
            public void onPmvilFailure(String pmvilMod, String pmvilError) {
                tostPmvilErrMsg(pmvilError);
            }
        });
        getPmvilPInset("pmvilblock","pmvilIsebank");
    }

    private void getPmvilPInset(String pmvilse,String pmvilLis){
        PmvilHttpRequest.getPmvilPInset(this, pmvilse, pmvilLis, new PmvilCallBack<String>() {
            @Override
            public void onPmvilSucc(String pmvilMethod, String pmvilContent) {

            }

            @Override
            public void onPmvilLogOn(String pmvilMod, String pmvilCtent) {

            }

            @Override
            public void onPmvilOther(String pmvilMhod, String pmvilCnt, String pmvilCode, String pmvilMsg) {

            }

            @Override
            public void onPmvilFailure(String pmvilMod, String pmvilError) {

            }
        });
    }
    @Override
    public void onBackPressed() {
        showPmvilWaliuDlg();
    }

    private void getBankPmvilList(){
        PmvilHttpRequest.getBankPmvilList(this, new PmvilCallBack<PmvilBanks>() {
            @Override
            public void onPmvilSucc(String pmvilMethod, PmvilBanks pmvilContent) {
                pmvilbankList.addAll(pmvilContent.getPmvilbankList());
                getBankPmvilType();
            }

            @Override
            public void onPmvilLogOn(String pmvilMod, PmvilBanks pmvilCtent) {
                outpmvilLongin();
            }

            @Override
            public void onPmvilOther(String pmvilMhod, PmvilBanks pmvilCnt, String pmvilCode, String pmvilMsg) {
                    tostPmvilErrMsg(pmvilMsg);
            }

            @Override
            public void onPmvilFailure(String pmvilMod, String pmvilError) {
                tostPmvilErrMsg(pmvilError);
            }
        });
    }

    private void getBankPmvilType(){
        PmvilHttpRequest.getPmvilConfig(this, "accountType", new PmvilCallBack<PmvilSerConfig>() {
            @Override
            public void onPmvilSucc(String pmvilMethod, PmvilSerConfig pmvilContent) {
                hindPmvilLoading();
                pmvilextInfoArr.addAll(pmvilContent.getPmvilextInfoArr());
            }

            @Override
            public void onPmvilLogOn(String pmvilMod, PmvilSerConfig pmvilCtent) {
                outpmvilLongin();
            }

            @Override
            public void onPmvilOther(String pmvilMhod, PmvilSerConfig pmvilCnt, String pmvilCode, String pmvilMsg) {
                tostPmvilErrMsg(pmvilMsg);
            }

            @Override
            public void onPmvilFailure(String pmvilMod, String pmvilError) {
                tostPmvilErrMsg(pmvilError);
            }
        });
    }

    private void showBankPmvilTypeDgl(LinearLayout sourcePmvilLl, TextView sourcePmvilTv, String sourcePmvilStr, List<PmvilSerConfig.ExtInfoArrPmvil> pmvilSourc) {
        View soucrPmvilView = LayoutInflater.from(this).inflate(R.layout.source_pmvil_pop_ly, null, false);
        final PopupWindow soucrPmvilPop = new PopupWindow(soucrPmvilView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, true);
        soucrPmvilPop.setTouchable(true);
        soucrPmvilPop.showAsDropDown(sourcePmvilLl);
        ImageView soucrPmvilPopIvback = soucrPmvilView.findViewById(R.id.source_pmvil_pop_iv_back);
        TextView soucrPmvilPopTvtitle = soucrPmvilView.findViewById(R.id.source_pmvil_pop_tv_title);
        RecyclerView soucrPmvilPopRecycle = soucrPmvilView.findViewById(R.id.source_pmvil_pop_recycle);
        soucrPmvilPopTvtitle.setText(sourcePmvilStr);
        soucrPmvilPopIvback.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                soucrPmvilPop.dismiss();
            }
        });
        PmvilSourceFiter pmvilSourceFiter = new PmvilSourceFiter(pmvilAy, pmvilSourc);

        soucrPmvilPopRecycle.setAdapter(pmvilSourceFiter);
        pmvilSourceFiter.setSourceFilerIngter(new PmvilSourceFiter.SourceFilerIngter() {
            @Override
            public void setPmvilSourCheckBean(PmvilSerConfig.ExtInfoArrPmvil pmvilSourPopBen) {
                sourcePmvilTv.setText(pmvilSourPopBen.getPmvilname());
                pmvilBankTypecode = pmvilSourPopBen.getPmvilcode();
                soucrPmvilPop.dismiss();
            }
        });
    }

    private void showBankPmvilListDgl(LinearLayout sourcePmvilLl, TextView sourcePmvilTv, String sourcePmvilStr, List<PmvilBanks.BankListPmvil> pmvilSourc) {
        View bankPmvilView = LayoutInflater.from(this).inflate(R.layout.source_pmvil_pop_ly, null, false);
        final PopupWindow bankPmvilPop = new PopupWindow(bankPmvilView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, true);
        bankPmvilPop.setTouchable(true);
        bankPmvilPop.showAsDropDown(sourcePmvilLl);
        ImageView soucrPmvilPopIvback = bankPmvilView.findViewById(R.id.source_pmvil_pop_iv_back);
        TextView soucrPmvilPopTvtitle = bankPmvilView.findViewById(R.id.source_pmvil_pop_tv_title);
        RecyclerView soucrPmvilPopRecycle = bankPmvilView.findViewById(R.id.source_pmvil_pop_recycle);
        soucrPmvilPopTvtitle.setText(sourcePmvilStr);
        soucrPmvilPopIvback.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                bankPmvilPop.dismiss();
            }
        });
        PmvilBankFiter pmvilBankeFiter = new PmvilBankFiter(pmvilAy, pmvilSourc);
        soucrPmvilPopRecycle.setAdapter(pmvilBankeFiter);
        pmvilBankeFiter.setBankFilerIngter(new PmvilBankFiter.BamlFilerIngter() {
            @Override
            public void setPmvilBankCheckBean(PmvilBanks.BankListPmvil pmvilBankPopBen) {
                sourcePmvilTv.setText(pmvilBankPopBen.getPmvilbankName());
                pmvilBankbean = pmvilBankPopBen;
                bankPmvilPop.dismiss();
            }
        });
    }

}
